Previous Next Contents Generated Index Doc Set



CHAPTER 11 : Generated Code


Introduction

Code can be generated from the Class Editor and from the Resource Bundle Editor. This section describes code generation from the Class Editor. Generating code from the Resource Bundle Editor is described in the Generating Code section on page 143.


How to Generate Code

To generate code for your classes, choose "Generate java..." from the Generate menu in the Class Editor or press the Generate button on the toolbar. The Directory Selection dialog is displayed, as shown in Figure 11-1. This allows you to specify in which directory you wish the generated code to appear.

Figure  11-1 Directory Selection dialog

You should note that Visaj adheres to the requirements of the Java language by using the name of the class as the filename for the Java source file. If, therefore, you generate a Java source file, change the name of the class in Visaj and then regenerate, a Java source file is created with the new filename reflecting the new class name. In addition, if you have the "Update existing files" toggle in the Generate dialog selected, the previous Java source file is removed. This is to ensure that the design and the generated code are always in step. If you wish to retain the previously generated Java source file, make sure that the "Update existing files" toggle is not set before generating. In this case, however, any changes you may have made to the generated source are lost.


What is Generated

For each class you have created in Visaj, a java file is generated. Each class is placed in a file of its own using the name of the class with a ".java" suffix. Note that you can only specify one class per Class Editor save file so one ".vcl" file corresponds to one Java class file.

In addition, if you have specified an object initialization type of "Deserialization" in the property editor of any object, the file specified there is generated too. Object initialization is described in more detail in the Object Initialization section on page 63. The file should have the suffix ".ser" to indicate that it is a serialized bean file. You are prompted to do this in the property editor.

Packing Frames

You should note that if you do not explicitly set the size property on a Frame (or JFrame from the Swing set of components), then the generated code includes a call to the pack() method. If you do set size property, then no call to the pack() method is generated.


Adding Your Own Code - Subclassing

Although you can edit the generated code directly to add your own code (this is explained in the following section), you may wish to consider subclassing the generated class file. This means that any code you add is in a separate file. It is easier to read than the generated code, which is sprinkled with protective comments. There is also no danger that the code you add will be accidentally lost when code is regenerated from Visaj.

Your new class would look something like this:

public NewClass extends VisajGeneratedClass {
	public void newMethod() {
		super.newMethod();
	}
}

There are a number of points to keep in mind when subclassing the generated code:


Editing the Code

You may edit the generated code - for example, to add an interface to existing software. Make sure, when you do this, that you do not edit anything in sections which begin with the special comment //vj+ and end with the special comment //vj-. Lines on their own which must not be changed or removed have the special comment //vj=. If your additions are made outside of these comments, they will be retained if you need to generate the code again.


Regenerating Code - Using the Update Toggle

When you regenerate your code, the Generate dialog contains a toggle labelled "Update existing file", as shown in Figure 11-2. This will ensure that any changes you have made to the generated code will be retained.

If you have generated source code and then changed the class name, when you next generate code the "Update existing file" toggle has a more dramatic effect. If this toggle is set, the file is updated to use the new class name as the source code filename. It then appears as though a new file has been created and the old one removed. If you wish to retain the previously generated source file, make sure that the "Update existing file" toggle is not set before you generate. Of course, in this case you would lose any changes you may have made to the source file.

If you use the toolbar Generate button to regenerate code, the Generate dialog does not appear. Instead, code is regenerated immediately, retaining any changes you may have made to it.

Figure  11-2 Directory Selection Dialog with Update Option


Example Code

The following is an example of code generated from a simple design, the containment hierarchy of which is shown in Figure 11-3. One event binding has been added to set the text of the textfield to "hello world" when button1 is pressed. The constructor has been designated "main method". Otherwise, all defaults have been retained.

This code compiles and runs. Because it uses defaults, the layouts and sizes would need to be changed to create a better appearance.

//vj+ <VJ-PackageName>
//vj- <VJ-PackageName>




//vj+ <VJ-PackageInclude>
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Frame;
import java.awt.Button;
import java.awt.Panel;
import java.awt.Label;
import java.awt.TextField;
import java.awt.MenuBar;
import java.awt.Menu;
import java.awt.MenuItem;
//vj- <VJ-PackageInclude>




//vj= <VJ-DefineClasses>
//vj+ <VJ-BeginClassDef>
public class MyClass extends Object   {
//vj- <VJ-BeginClassDef>




    //vj+ <VJ-DataMembers>
    protected Frame frame1 ;
    protected Button button1 ;
    protected Button button2 ;
    protected Panel panel1 ;
    protected Label label1 ;
    protected TextField textField1 ;
    protected MenuBar menuBar1 ;
    protected Menu menu1 ;
    protected MenuItem menuItem1 ;
    protected MenuItem menuItem2 ;
    protected Menu menu2 ;
    protected MenuItem menuItem3 ;
    //vj- <VJ-DataMembers>




    //vj= <VJ-Methods>




    //vj+ <VJ-BeginMethodDef>
    // Method# 1
    public  MyClass()  {
    //vj- <VJ-BeginMethodDef>




        //vj= <VJ-MethodCode>
        //vj+ <VJ-DefineAWTMembers>
        frame1 = new Frame();
        frame1.setTitle( "frame1" );
        button1 = new Button();
        button1.setLabel( "button1" );
        button2 = new Button();
        button2.setLabel( "button2" );
        panel1 = new Panel();
        label1 = new Label();
        label1.setText( "label1" );
        textField1 = new TextField();
        panel1.add(label1, null, -1);
        panel1.add(textField1, null, -1);
        menuBar1 = new MenuBar();
        menu1 = new Menu();
        menu1.setLabel( "menu1" );
        menuItem1 = new MenuItem();
        menuItem1.setLabel( "menuItem1" );
        menuItem2 = new MenuItem();
        menuItem2.setLabel( "menuItem2" );
        menu1.add(menuItem1);
        menu1.add(menuItem2);
        menu2 = new Menu();
        menu2.setLabel( "menu2" );
        menuItem3 = new MenuItem();
        menuItem3.setLabel( "menuItem3" );
        menu2.add(menuItem3);
        menuBar1.add(menu1);
        menuBar1.add(menu2);
        {
            String strConstraint;
            strConstraint = "Center";
            frame1.add(button1, strConstraint, -1);
            strConstraint = "North";
            frame1.add(button2, strConstraint, -1);
            strConstraint = "South";
            frame1.add(panel1, strConstraint, -1);
        }
        frame1.setMenuBar(menuBar1);
        frame1.pack();
        frame1.show();
        //vj- <VJ-DefineAWTMembers>




        //vj+ <VJ-EndAWT>
        //vj- <VJ-EndAWT>








        //vj+ <VJ-EventListenerClass>
        class ActionListenerAdapter implements ActionListener  {
            public void actionPerformed( ActionEvent e )  {
                if ( e.getSource().equals( button1 ) )  {
                    textField1.setText( "Hello world" );
                    return;
                }
            }
        }
        //vj- <VJ-EventListenerClass>




        //vj+ <VJ-AddEventListeners>
        button1.addActionListener(new ActionListenerAdapter());
        //vj- <VJ-AddEventListeners>




        //vj= <VJ-Classes>




    //vj+ <VJ-EndMethodDef>  
    }
    //vj- <VJ-EndMethodDef>  




    //vj= <VJ-Classes>




//vj+ <VJ-EndClassDef>  
    public static void main (String args[]) { 
        try  {
            MyClass myclass = new MyClass();
        } catch ( Exception ex )  {
            ex.printStackTrace();
        }
    }
}
//vj- <VJ-EndClassDef>

Figure  11-3 Hierarchy for Sample Code


Using the Diamond Components

If you have used any of the Diamond components in your application, you will have to make sure that the library or directory containing the Diamond class files is in your CLASSPATH. For the current release, the Diamond class files are in the file diamonds.jar, which is found in the Visaj install directory.


File Types on Apple Macintoshes

If you intend to run the generated code on an Apple Macintosh, you will need to set the file type of the generated code files. This is to enable them to be recognized by your chosen IDE. Visaj uses the following system property to decide which file type to set:

vj.macJavaFileType

This defaults to "CWIE", which is the type expected by the IDE Code Warrior.




Previous Next Contents Generated Index Doc Set

Copyright © 1998, 1999 Pacific Imperial Inc., TakeFive Software Inc. All Rights Reserved.